//
//  MTSelfMenuVC.m
//  Travel
//
//  Created by xiaoai on 15/11/19.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MTSelfMenuVC.h"
#import "UIImage+BlurGlass.h"
#import "YRSideViewController.h"

#import "TestViewController.h"
#import "InformationDetailVC.h"
#import "AboutAppDetailVC.h"
#import "MyCollectDetailVC.h"
#import "MyBarViewController.h"



@interface MTSelfMenuVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;

//存放照片的背景
@property (nonatomic,strong)UIView *backView;

//图片
@property (nonatomic,strong)UIImageView *myImageView;
//姓名
@property (nonatomic,strong)UILabel *nameLabel;

//心情寄语
@property (nonatomic,strong)UITextField *noteText;

//二维码的按钮
@property (nonatomic,strong)UIButton *barBtn;


//存放按钮的
@property (nonatomic,strong)UIView *footView;
//注销按钮
@property (nonatomic,strong)UIButton *cancleBtn;


//cell显示值的方法
@property (nonatomic,strong)NSArray *titleArr;

//夜间模式的控制按钮
@property (nonatomic,strong)UISwitch *switchBack;

@end

@implementation MTSelfMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //毛玻璃背景图片
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    

//退出按钮
//@property (nonatomic,strong)UIButton *quitBtn;
@end

@implementation MTSelfMenuVC




- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];

    backImageView.contentMode = UIViewContentModeScaleToFill;
    
    UIImage *image =[[UIImage imageNamed:@"mao"] imgWithLightAlpha:0.5 radius:9 colorSaturationFactor:1.8];

    backImageView.userInteractionEnabled = YES;

    [backImageView setImage:image];
    
    [self.view addSubview:backImageView];
    
    

    self.titleArr = @[@"个人资料",@"我的收藏",@"我的二维码",@"夜间模式",@"关于旅记"];
    
    //设置头部视图
    [self designHeadViewTextName:@"小艾" noteTextPlaceHolder:@"心情寄语。。。。。" showText:nil];
    
    //设置tableView
    [self designTableView];
    
    //设置脚部视图
    [self designFootView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(tapAction:)];

    //将手势添加到头部视图上
    [self.backView addGestureRecognizer:tap];

    [self.view addGestureRecognizer:tap];

    
}

//手势方法
-(void)tapAction:(UITapGestureRecognizer *)tapGest{

    [self.view endEditing:YES];

}
-(void)designHeadViewTextName:(NSString *)name noteTextPlaceHolder:(NSString *)noteTextPlayHolder showText:(NSString *)showText{
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(40, 10, 250, 250)];
    
    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 30, 150, 150)];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 200, 80, 30)];
    
    //设置头像和名字
    [self setMyImageView:self.myImageView Label:self.nameLabel Text:name fontName:@"HelveticaNeue" size:17 view:self.backView];
    self.noteText = [[UITextField alloc]initWithFrame:CGRectMake(10, 250, 200, 30)];
    
    self.noteText.borderStyle = UITextBorderStyleNone;
    
    self.noteText.backgroundColor = [UIColor clearColor];
    //设置代理
    self.noteText.delegate = self;
    
    self.noteText.text = showText;
    
    //设置隐藏字体
    self.noteText.placeholder = noteTextPlayHolder;
    
    //设置文字居中
    [self.noteText setTextAlignment:NSTextAlignmentCenter];

    
    [self.view addSubview:self.noteText];
    
    //将背景view添加到
    [self.view addSubview:self.backView];
    
}

//设置label
-(void)setMyImageView:(UIImageView *)myImageView Label:(UILabel *)label Text:(NSString *)textName fontName:(NSString *)fontName size:(CGFloat)size view:(UIView *)view{
    
    
    //设置图片
    myImageView.image = [UIImage imageNamed:@"chang"];
    
    myImageView.layer.masksToBounds = YES;
    
    myImageView.layer.cornerRadius = self.myImageView.frame.size.width * 0.5;
    
    myImageView.layer.borderColor = [UIColor colorWithRed:0.295 green:0.570 blue:0.667 alpha:1.000].CGColor;
    
    myImageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    myImageView.layer.shouldRasterize = YES;
    
    myImageView.clipsToBounds = YES;
    
    
    //设置label的名字
    label.text = textName;
    //设置label的背景
    label.backgroundColor = [UIColor clearColor];
    //设置label的字体颜色
    label.textColor = [UIColor colorWithRed:62/255.0f green:68/255.0f blue:75/255.0f alpha:1.0f];
    //
    [label sizeToFit];
    
    [view addSubview:myImageView];
    [view addSubview:label];
    
}

-(void)designTableView{
    //初始化
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 280, 300, 280) style:UITableViewStylePlain];
    
    //把背景色去掉
    self.tableView.backgroundColor = [UIColor clearColor];
    
    //设置代理
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //设置分割线为空
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    //cell注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //添加
    [self.view addSubview:self.tableView];
}

-(void)designFootView{
    
    self.footView = [[UIView alloc]initWithFrame:CGRectMake(0, 550, 300, 55)];
    
    self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.cancleBtn.frame = CGRectMake(20, 10, 80, 40);
    
    [self.cancleBtn setImage:[[UIImage imageNamed:@"tuichu"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

    [self.cancleBtn addTarget:self action:@selector(cancleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.footView addSubview:self.cancleBtn];
    
    [self.view addSubview:self.footView];
    
}
#warning ---- 退出登陆
-(void)cancleBtnClick:(UIButton *)cancleBtn{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"您确定要退出当前账号吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
#warning ---- 退出之后头像内容都要清除
        
        NSLog(@"退出");
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alertVC addAction:cancleAction];
    [alertVC addAction:okAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
    
}
    [self.footView addSubview:self.cancleBtn];
    
    [self.view addSubview:self.footView];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

//分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    //去掉cell的背景颜色
   cell.backgroundColor = [UIColor clearColor];
    
    // 设置选中cell的背景色
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.476 alpha:0.153];
    
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    //设置每个cell的显示值
    cell.textLabel.text = self.titleArr[indexPath.row];
    
    if (indexPath.row == 3) {
        //开关按钮
        self.switchBack = [[UISwitch alloc]initWithFrame:CGRectMake(150, 10, 100, 30)];
        
        [self.switchBack setOn:NO animated:YES];
        
        [self.switchBack addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        
        [cell addSubview:self.switchBack];
    }
    

    cell.backgroundColor = [UIColor clearColor];
    //
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *titleArr = @[ @"个人资料",@"我的收藏",@"我的二维码",@"夜间模式",@"关于旅记"];
    
    cell.textLabel.text = titleArr[indexPath.row];

    
    return cell;
}


#pragma mark --- 夜间模式开关
-(void)switchChange:(UISwitch *)switchBack{
    
    if (self.switchBack.isOn) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"即将进入夜间模式,\n你准备好了吗？" preferredStyle:UIAlertControllerStyleAlert];
        //确定按钮
        UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
#warning ----此处要开启夜间模式设置
            
        }];
        //取消按钮
        UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"算了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            [self.switchBack setOn:NO animated:YES];
            
        }];
        //添加
        [alertVC addAction:actionOK];
        [alertVC addAction:actionCancle];
        
        //模态
        [self presentViewController:alertVC animated:YES completion:nil];
    
    }else{
#warning ------此处要添加返回到非夜间模式
    //此处设置返回到非夜间模式
    
    }

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    //失去第一响应者
    [self.noteText resignFirstResponder];
    return YES;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
        {
            //个人资料
            InformationDetailVC *infoDVC = [InformationDetailVC new];
          [self presentToDetailVC:infoDVC SelectRow:0];

        }
            break;
            
        case 1:{
            //我的收藏
            MyCollectDetailVC *collectDVC = [MyCollectDetailVC new];

            [self presentToDetailVC:collectDVC SelectRow:1];
        }
            break;
            
        case 2:
        {
            //我的二维码
             MyBarViewController *barDVC = [MyBarViewController new];
            [self presentToDetailVC:barDVC SelectRow:2];
        }
            break;
        case 3:{
         //调用switch开关改变的方法
            [self changeSwitchState];
            
        }
            break;
            
            
        case 4:
        {
            //关于旅记
            AboutAppDetailVC *aboutDVC = [AboutAppDetailVC new];
         [self presentToDetailVC:aboutDVC SelectRow:4];
        }
            break;
        default:
            break;
    }
    
    
}

//点击cell改变switch开关
-(void)changeSwitchState{

    [self.switchBack setOn:!self.switchBack.isOn animated:YES];
    
    //调用switch开关自己的方法
    [self switchChange:nil];

}


//模态到下一页的方法
-(void)presentToDetailVC:(UIViewController *)viewController SelectRow:(NSUInteger)integer{

    
    viewController.title = self.titleArr[integer];
    
    viewController.view.backgroundColor = [UIColor  whiteColor];
    
    UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:viewController];
    
    //设置模态动画效果
    viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    [self presentViewController:navc animated:YES completion:nil];

}

@end
